package com.yvan.platform;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yvan.Conv;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    private static final Pattern PATTERN = Pattern.compile("(\\$\\{.*?\\})");
    public static final String EMPTY_STRING = "";
    private static final String EMPTY = "";


    /**
     * 判断是否为null或空字符串。如果不为null，在判断是否为空字符串之前会调用trim()。
     */
    public static boolean isNullOrEmpty(String str) {
        return str == null || str.trim().equals(EMPTY);
    }

    /**
     * 判断是否为null或空字符串。如果不为null，在判断是否为空字符串之前会调用trim()。
     */
    public static boolean isNullOrEmpty(Object object) {

        if (object == null) {
            return true;
        }
        return isNullOrEmpty(object.toString());
    }

    public static boolean isNotEmpty(String value) {
        return !isNullOrEmpty(value);
    }

    /**
     * 用 jsonWapper / Map 进行字符串的格式化<br />
     * /${env}/projects/${project}/${segment}/config"
     */
    public static String format(final String fmtString, final Map<String, ?> model) {
        Matcher match = PATTERN.matcher(fmtString);
        int countAll = match.groupCount();

        List<Object[]> grp = Lists.newArrayList();

        while (match.find()) {
            for (int i = 0; i < countAll; i++) {
                grp.add(new Object[]{
                        match.group(i + 1),
                        match.start(i + 1),
                        match.end(i + 1)
                });
            }
        }

        StringBuffer buffer = new StringBuffer(fmtString);
        for (int i = grp.size() - 1; i >= 0; i--) {
            final String pts = (String) grp.get(i)[0];
            final String key = pts.substring(2, pts.length() - 1);
            buffer.replace((int) grp.get(i)[1], (int) grp.get(i)[2], Conv.NS(model.get(key)));
        }

        return buffer.toString();
    }

    /**
     * 用 jsonWapper / Map 进行字符串的格式化<br />
     * /${env}/projects/${project}/${segment}/config
     */
    public static String format(final String fmtString, final JsonWapper jsonWapper) {
        return format(fmtString, jsonWapper.getInnerMap());
    }

    public static void main(String[] args) {
        String v =
                format("/${env}/projects/${project}/${segment}/config", new ResultModel()
                        .set("env", "dev")
                        .set("project", "dm-1")
                        .set("segment", "springboot")
                        .set("a", "1"));
        System.out.println(v);

        String v2 =
                format("/${env}/projects/${project}/${segment}/config", new JsonWapper()
                        .set("env", "dev")
                        .set("project", "dm-1")
                        .set("segment", "springboot")
                        .set("a", "1"));

        System.out.println(v2);
    }

    public static FmtBuilder format(String fmtString) {
        return new FmtBuilder(fmtString);
    }

    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    public static class FmtBuilder {

        private final String fmtString;
        private final Map<String, Object> param = Maps.newLinkedHashMap();

        public FmtBuilder(String fmtString) {
            this.fmtString = fmtString;
        }

        public FmtBuilder set(String key, Object value) {
            param.put(key, value);
            return this;
        }

        public String build() {
            return format(fmtString, param);
        }
    }
}
